package org.jeecg.modules.wangyue.controller.sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.CommonConstant;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.UserBasic;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.service.IUserBasicService;
import org.jeecg.modules.wangyue.service.IUserRoleInfoService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 用户管理
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags="用户管理")
@RestController
@RequestMapping("/wangyue/sys/userManage")
public class UserManageController extends JeecgController<UserRoleInfo, IUserRoleInfoService> {
	@Autowired
	private IUserRoleInfoService userRoleInfoService;
	@Autowired
	private IUserBasicService userBasicService;
	@Autowired
	private ExportUtil exportUtil;


	 /**
	  * 用户管理-分页列表查询
	  * @param keywords		关键字
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	@AutoLog(value = "用户管理-分页列表查询")
	@ApiOperation(value="用户管理-分页列表查询", notes="用户管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(@RequestParam(name="keywords", required = false) String keywords,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Page<UserRoleInfo> page = new Page<UserRoleInfo>(pageNo, pageSize);
		IPage<UserRoleInfo> pageList = userRoleInfoService.getUserRoleInfoByPage(page, RoleConstant.yonghu, keywords);
		return Result.OK(pageList);
	}
	

	
	/**
	 * 用户管理-拉黑/取消操作
	 *
	 * @param userBasic
	 * @return
	 */
	@AutoLog(value = "用户管理-拉黑/取消操作")
	@ApiOperation(value="用户管理-拉黑/取消操作", notes="用户管理-拉黑/取消操作")
	@PostMapping(value = "/editBlock")
	public Result<?> editBlock(@RequestBody UserBasic userBasic) {
		UpdateWrapper<UserBasic> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", userBasic.getId());
		updateWrapper.eq("del_flag", DelFlagConstant.normal);
		updateWrapper.set("is_block", userBasic.getIsBlock());
		if(userBasicService.update(updateWrapper)){
			return Result.OK("操作成功");
		}
		return Result.error("操作失败");
	}


	 /**
	  * 导出excel
	  *
	  * @param userRoleInfo
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(UserRoleInfo userRoleInfo) {
	 	 List<String> idList = null;
	 	 if(oConvertUtils.isNotEmpty(userRoleInfo.getIds())){
	 	 	 idList = Arrays.asList(userRoleInfo.getIds().split(","));
		 }
		 List<UserRoleInfo> userRoleInfoList = userRoleInfoService.getUserRoleInfoByList(RoleConstant.yonghu, idList, userRoleInfo.getKeywords());

		 //处理url数据，添加文件地址前缀
		 if(oConvertUtils.isNotEmpty(userRoleInfoList) && userRoleInfoList.size() > 0){
			 for(UserRoleInfo uri : userRoleInfoList){
				 if (oConvertUtils.isNotEmpty(uri.getHeadPicUrl())){
					 uri.setHeadPicUrl(CommonConstant.fileAddressPrefix + uri.getHeadPicUrl());
				 }
				 if (oConvertUtils.isNotEmpty(uri.getIdCardFrontUrl())){
					 uri.setIdCardFrontUrl(CommonConstant.fileAddressPrefix + uri.getIdCardFrontUrl());
				 }
				 if (oConvertUtils.isNotEmpty(uri.getIdCardBackUrl())){
					 uri.setIdCardBackUrl(CommonConstant.fileAddressPrefix + uri.getIdCardBackUrl());
				 }
				 if (oConvertUtils.isNotEmpty(uri.getWorkProveUrl())){
					 uri.setWorkProveUrl(CommonConstant.fileAddressPrefix + uri.getWorkProveUrl());
				 }
				 if (oConvertUtils.isNotEmpty(uri.getHealthProveUrl())){
					 uri.setHealthProveUrl(CommonConstant.fileAddressPrefix + uri.getHealthProveUrl());
				 }
			 }
		 }

		 return exportUtil.exportXls(userRoleInfoList, UserRoleInfo.class, "用户信息数据");
	 }


}
